package com.lsx.logisticscore.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lsx.logistics.IdUtil;
import com.lsx.logistics.MD5Util;
import com.lsx.logisticscore.entity.Account;
import com.lsx.logisticscore.entity.Employee;
import com.lsx.logisticscore.mapper.AccountMapper;
import com.lsx.logisticscore.mapper.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

@Service
public class AccountService {
    @Autowired
    private AccountMapper accountMapper ;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private EmployeeService employeeService;
    public int deleteAccount(Integer accountId){
        //如果该账号的员工仍存在,就不能删除
        Account account = accountMapper.selectById(accountId);
        Employee employee = employeeService.getEmployeeById(account.getEmpId());
        if (employee != null){
            return -1;
        }
        return accountMapper.deleteById(accountId);
    }

    /**
     * 新建账号信息
     * @param account
     * @return 如果成功，返回账号信息
     */
    public Account saveAccount(Account account){
        //account中包含的信息：员工id，员工名，罢了。
        //必须是先有员工后有账号
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.eq("emp_id", account.getEmpId());
        Employee employee = employeeMapper.selectOne(wrapper);
        if (employee == null){
            return null;
        }
        account.setType(employee.getType());
        account.setUsername(employee.getName());
        account.setEmpName(employee.getName());
        account.setRole("manager");
        account.setRegistryCode(IdUtil.getRegistryCode());
        int result = accountMapper.insertSelective(account);
        //返回其实主要也就是返回个注册码信息
        if (result >= 1){
            return account;
        }
        return null;
    }

    /**
     * 初始化账号信息
     * @param account
     * @return
     */
    public Account initAccount(Account account){
        account.setPassword(MD5Util.getMd5(account.getPassword(), account.getEmpId()));
        account.setRegistryDate(new Date());
        QueryWrapper<Account> qw = new QueryWrapper<>();
        qw.lambda().eq(Account::getEmpId, account.getEmpId());
        accountMapper.update(account, qw);
        account.setPassword(null);
        return account;
    }

    /**
     * 根据注册码激活账号
     * @param registryCode
     * @return
     */
    public Account activeAccount(String registryCode, String empId){
        QueryWrapper <Account> wrapper = new QueryWrapper<>();
        wrapper.eq("emp_id", empId);
        Account account = accountMapper.selectOne(wrapper);
        if (account == null || !account.getRegistryCode().equals(registryCode)){
            return null;
        }
        return account;

    }


    /**
     * 改密码
     */
    public int changePassword(JSONObject params){
        String empId = params.getString("empId");
        String oldPass = params.getString("oldPwd");
        String newPass = params.getString("newPwd");

        QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Account::getEmpId, empId);
        Account account = accountMapper.selectOne(queryWrapper);
        if (Objects.isNull(account)) {
            return -1;
        }
        if (Objects.nonNull(account.getPassword()) && (account.getPassword().equals(MD5Util.getMd5(oldPass, empId)))){
            account.setPassword(MD5Util.getMd5(newPass, empId));
            accountMapper.update(account, queryWrapper);
            return 0;
        }else {
            return -2;
        }
    }

}
